package com.jike.jkgame.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.jike.jkgame.JKApp;
import com.jike.jkgame.data.ResourceType;
import com.jike.jkgame.data.Status;
import com.jike.jkgame.data.UserResource;
import com.jike.jkgame.data.UserStatus;

public class UserDAO {
	private SQLiteDatabase mDatabase;
	private GameSQLiteHelper mDBHelper;

	private static UserDAO mInstance;

	public static UserDAO get() {
		if (mInstance == null) {
			mInstance = new UserDAO(JKApp.get());
			mInstance.open();
		}
		return mInstance;
	}

	private UserDAO(Context context) {
		mDBHelper = new GameSQLiteHelper(context);
	}

	public void open() {
		if (mDatabase == null) {
			mDatabase = mDBHelper.getWritableDatabase();
		}
	}

	public void close() {
		mDBHelper.close();
	}

	public long saveStatus(UserStatus status) {
		if (status == null) {
			return -1;
		}

		ContentValues values = new ContentValues();
		values.put("uid", status.getUid());
		values.put("level", status.getLevel());

		Status st = status.getEnergy();
		values.put("energy", st.value);
		values.put("max_energy", st.maxValue);
		values.put("recovery_energy", st.recoverValue);
		values.put("time_energy", st.recoverTime);
		values.put("time_remain_energy", st.timeUntillRecover);

		st = status.getVigour();
		values.put("vigour", st.value);
		values.put("max_vigour", st.maxValue);
		values.put("recovery_vigour", st.recoverValue);
		values.put("time_vigour", st.recoverTime);
		values.put("time_remain_vigour", st.timeUntillRecover);

		// st = status.getAmmo();
		// values.put("ammo", st.value);
		// values.put("max_ammo", st.maxValue);
		// values.put("recovery_ammo", st.recoverValue);
		// values.put("time_ammo", st.recoverTime);
		// values.put("time_remain_ammo", st.timeUntillRecover);

		values.put("timestamp", System.currentTimeMillis());

		Cursor cursor = mDatabase.query(UserStatusTable.TABLE_NAME, null,
				"uid = ?", new String[] { String.valueOf(status.getUid()) },
				null, null, null);

		long insertId = -1;
		if (cursor.getCount() <= 0) {
			insertId = mDatabase.insert(UserStatusTable.TABLE_NAME, null,
					values);
		} else {
			insertId = mDatabase
					.update(UserStatusTable.TABLE_NAME, values, "uid = ?",
							new String[] { String.valueOf(status.getUid()) });
		}

		cursor.close();

		return insertId;
	}

	public void restoreUserStatus() {
		UserStatus status = UserStatus.get();
		Cursor cursor = mDatabase.query(UserStatusTable.TABLE_NAME, null,
				"uid = ?", new String[] { String.valueOf(status.getUid()) },
				null, null, null);
		cursor.moveToFirst();
		if (cursor.isAfterLast()) {
			cursor.close();
			return;
		}

		long past = System.currentTimeMillis() - cursor.getLong(13);

		status.setLevel(cursor.getInt(2));
		Status st = status.getEnergy();
		st.value = cursor.getInt(3);
		st.maxValue = cursor.getInt(4);
		st.recoverValue = cursor.getInt(5);
		st.recoverTime = cursor.getInt(6);
		st.timeUntillRecover = cursor.getLong(7);
		st.passTime(past);

		st = status.getVigour();
		st.value = cursor.getInt(8);
		st.maxValue = cursor.getInt(9);
		st.recoverValue = cursor.getInt(10);
		st.recoverTime = cursor.getInt(11);
		st.timeUntillRecover = cursor.getLong(12);
		st.passTime(past);

		cursor.close();
	}

	public long insertOrReplaceUserResource(UserResource res) {
		if (res == null) {
			return -1;
		}

		ContentValues v = new ContentValues();
		v.put("res", res.res.ordinal());
		v.put("tid", res.tid);
		v.put("sid", res.sid);
		v.put("level", res.level);
		v.put("exp", res.exp);
		v.put("attack", res.attack);
		v.put("defense", res.defense);
		v.put("star", res.star);
		v.put("type", res.type);
		v.put("add_val", res.add);
		v.put("cast", res.cast);

//		Cursor cursor = mDatabase.query(UserResourcesTable.TABLE_NAME, null,
//				"sid=?", new String[] { String.valueOf(res.sid) }, null, null,
//				null);
//		
//		cursor.moveToFirst();
//		boolean exist = !cursor.isAfterLast();
//		cursor.close();
//		long id;
//		if (exist) {
//			id = mDatabase.insert(UserResourcesTable.TABLE_NAME, null, v);
//		} else {
//			id = mDatabase.update(UserResourcesTable.TABLE_NAME, v, "sid=?", new String[] { String.valueOf(res.sid) });
//		}
		long id = mDatabase.replace(UserResourcesTable.TABLE_NAME, null, v);

		return id;

	}

	private UserResource cursorToUserResource(Cursor cursor) {
		UserResource res = new UserResource();

		res.res = ResourceType.values()[cursor.getInt(1)];
		res.tid = cursor.getInt(2);
		res.sid = cursor.getInt(3);
		res.level = cursor.getInt(4);
		res.exp = cursor.getInt(5);
		res.attack = cursor.getInt(6);
		res.defense = cursor.getInt(7);
		res.star = cursor.getInt(8);
		res.type = cursor.getInt(9);
		res.add = cursor.getInt(10);
		res.cast = cursor.getInt(11);

		return res;
	}

	public UserResource getUserResource(int sid) {
		Cursor cursor = mDatabase.query(UserResourcesTable.TABLE_NAME, null,
				"sid=?", new String[] { String.valueOf("sid") }, null, null,
				null);

		cursor.moveToFirst();
		UserResource res = null;
		if (!cursor.isAfterLast()) {
			res = cursorToUserResource(cursor);
		}
		cursor.close();

		return res;
	}

}
